package com.firesrc.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jodd.util.StringTemplateParser;
import jodd.util.StringTemplateParser.MacroResolver;
import jodd.util.StringUtil;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JsoupUtil {

  private static StringTemplateParser stringTemplateParser;

  public static List<String> getBaiduUrlListFromKeyword(String string, String count) throws IOException {
    List<String> list = new ArrayList<String>();
    try {
      Document doc = Jsoup.connect("http://www.baidu.com/s?wd=" + string + "&cl=3&rn=" + count)
          .userAgent("Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)") // 设置User-Agent
          .cookie("auth", "token1") // 设置cookie
          .timeout(2000) // 设置连接超时时间
          .get();

      Elements resultLinks = doc.select("span.g");
      for (Element link : resultLinks) {
        String urlListFromText = cutRootUrl(link.text());
        if (urlListFromText.length() > 0 && urlListFromText.charAt(0) == 'w') {
          list.add(urlListFromText);
        }
      }
      Elements resultLinks1 = doc.select("[color=#008000]");
      for (Element link : resultLinks1) {
        String urlListFromText = cutRootUrl(link.text());
        if (urlListFromText.startsWith("w")) {
          list.add(urlListFromText);
        }
      }
    } catch (Exception e) {
      System.out.println("JsoupUtil.getBaiduUrlListFromKeyword()  连接错误");
    }
    return list;
  }

  public static String cutRootUrl(String text) {
    if (StringUtil.isEmpty(text)) {
      return "";
    }
    String[] split = StringUtil.split(text, "/");
    text = split[0];
    String[] split1 = StringUtil.split(text, " ");
    text = split1[0];
    return text;
  }

  public static Set getMailListFromText(String aa) {
    Set mailSet = new HashSet();
    Pattern pattern = Pattern.compile("[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+");
    Pattern pattern2 = Pattern.compile("[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?");
    Matcher matcher = pattern.matcher(aa);

    Matcher matcher2 = pattern2.matcher(aa);
    // Map<String, String> httpMap = new HashMap<String, String>();
    // while (matcher2.find()) {
    // System.err.println("JsoupUtil.pr()" + matcher2.group());
    // }
    while (matcher.find()) {
      mailSet.add(matcher.group());
    }
    return mailSet;
  }

  public static String getUrlListFromText(String aa) {
    String url = "";
    Pattern pattern2 = Pattern.compile("[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?");

    Matcher matcher2 = pattern2.matcher(aa);
    // Map<String, String> httpMap = new HashMap<String, String>();
    // while (matcher2.find()) {
    // System.err.println("JsoupUtil.pr()" + matcher2.group());
    // }
    while (matcher2.find()) {
      url = matcher2.group();
    }
    return url;
  }

  public static String getMailBodyHtml(String fileName) {
    String currentClassPath = getCurrentClassPath();
    String string = "mailcontent.html";
    String in = currentClassPath + "/" + string;
    File file = new File(in);
    Document parse = null;
    try {
      parse = Jsoup.parse(file, "utf-8");
    } catch (IOException e) {
      e.printStackTrace();
    }
    String html = parse.html();
    return html;
  }

  public static String getCurrentClassPath() {
    return Thread.currentThread().getContextClassLoader().getResource("").toString().substring(6);
  }

  public static String getTextFromTemplate(String body, final Map<String, String> map) {
    stringTemplateParser = new StringTemplateParser();
    String result = stringTemplateParser.parse(body, new MacroResolver() {
      public String resolve(String macroName) {
        return map.get(macroName);
      }
    });
    return result;
  }

  public static boolean checkNetConnect() throws IOException {
    Document document = null;
    try {
      document = Jsoup.connect("http://www.baidu.com").get();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      System.out.println("JsoupUtil.checkNetConnect() bd");
    }
    boolean b = document != null;
    return b;
  }
}
